package controller;

import com.google.gson.Gson;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import pojo.Blog;
import service.BlogService;
import utils.util;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/blog")
public class BlogController {
    @Resource
    private BlogService blogService;
    @RequestMapping("/addBlog")
    public String addBlog(@RequestParam Map<String, String> map) throws NoSuchAlgorithmException {
        String strBlog = map.get("strBlog");//获取安卓端传过来的键
        Gson gson = new Gson();
        Blog blog = gson.fromJson(strBlog, Blog.class);

        blog.setHeatNum(0);
        blog.setTime(util.getNowTime());
        int i = blogService.insert(blog);
        if (i > 0) {
            String JsonStr = new Gson().toJson(blog);
            return JsonStr;
        } else {
            return "fail";     //保存失败
        }
    }

    @RequestMapping("/deletemypublish")
    public void deletemypublish(@RequestParam Map<String, String> map) throws NoSuchAlgorithmException {
        String strdelete = map.get("strdelete");//获取安卓端传过来的键
        Gson gson = new Gson();
        Blog blog = gson.fromJson(strdelete, Blog.class);
        String blogid = blog.getBlogid();
        blogService.delete(blogid);

    }

    @RequestMapping(value = "blogdataget")
    public void getBlog(@RequestParam Map<String, Object> map, HttpServletRequest request, HttpServletResponse response) {
        List<Blog> blogList = new ArrayList<Blog>();
        String blogStr = "";
        blogList = blogService.selectBlog();
        System.out.println(blogList.size());
        System.out.println(blogList.get(2));
        Gson gson = new Gson();
        blogStr = new Gson().toJson(blogList);
        System.out.println(blogStr);
        try {
            response.setContentType("charset=UTF-8");
            response.getWriter().write(blogStr);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("getAllBlog")
    public String getAllBlog() {
        int blogNums = blogService.getBlogCount();
        String JsonStr = new Gson().toJson(blogNums);
        return JsonStr;

    }
    @CrossOrigin
    @RequestMapping("getBlogalljson")
    public void getBlogalljson( HttpServletResponse response){
        List<Blog> list = blogService.getBlogalljson();
        String JsonStr = new Gson().toJson(list);
        try {
            response.setContentType("charset=UTF-8");
            response.getWriter().write(JsonStr);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("getBlogCountByuserid")
    public String getBlogCountByuserid(@RequestParam Map<String, String> map,HttpServletResponse response) throws IOException {
        String Bloguserid=map.get("Bloguserid");
        Gson gson = new Gson();
        Blog blog = gson.fromJson(Bloguserid, Blog.class);
        String userid = blog.getUser().getUserid();

        int blogNums = blogService.getBlogCountByuserid(userid);
        String JsonStr = new Gson().toJson(blogNums);
        System.out.println(blogNums);
        return JsonStr;
    }

    //附近页
    @RequestMapping("getBlogPager")
    public void getBlogPager(@RequestParam Map<String, String> map, HttpServletResponse response) {
        String region = map.get("region");
        int pageNo = Integer.parseInt(map.get("pageNo"));
        List<Blog> list = blogService.getBlogPager(region, pageNo, 5);
        System.out.println(list);
        String JsonStr = new Gson().toJson(list);
        try {
            response.setContentType("charset=UTF-8");
            response.getWriter().write(JsonStr);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @RequestMapping("getBlogByUseID")
    public void getBlogByUseID(@RequestParam Map<String, String> map, HttpServletResponse response) {
        String strmypublish = map.get("strmypublish");
        Gson gson = new Gson();

        Blog blog = gson.fromJson(strmypublish, Blog.class);
        String userid = blog.getUser().getUserid();
        System.out.println("我的用户名：：：："+userid);
        List<Blog> list = blogService.getBlogByUserId(userid);
        System.out.println(list);
        String JsonStr = new Gson().toJson(list);
        try {
            response.setContentType("charset=UTF-8");
            response.getWriter().write(JsonStr);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    @RequestMapping("getHomeBlogPager")
    public void getHomeBlogPager(@RequestParam Map<String, String> map, HttpServletResponse response) {
        String types = map.get("types");
        int pageNo = Integer.parseInt(map.get("pageNo"));
        List<Blog> list = blogService.selectHomeBlog(types);
        System.out.println(types+" :的分页数据："+pageNo);
        System.out.println(list);
        String JsonStr = new Gson().toJson(list);
        try {
            response.setContentType("charset=UTF-8");
            response.getWriter().write(JsonStr);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @RequestMapping("getBlogDetail")
    public void getBlogDetail(@RequestParam Map<String, String> map, HttpServletResponse response) {
        String blogId = map.get("blogId");
        System.out.println(blogId);
        List<Blog> list = blogService.selectdetailBlog(blogId);
        Blog blog = list.get(0);
        String JsonStr = new Gson().toJson(blog);
        System.out.println(blog.toString());
        try {
            response.setContentType("charset=UTF-8");
            response.getWriter().write(JsonStr);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @RequestMapping("/getPersonBlogPager")
    public void getPersonBlogPager(@RequestParam Map<String, String> map,HttpServletResponse response){
        String personID=map.get("personID");
        int pageNo= Integer.parseInt(map.get("pageNo"));
        System.out.println("查询数据——————————————————"+pageNo);
        List<Blog> list=blogService.selectPersonBlog(personID,pageNo,5);
        System.out.println("person的数据——————————————————"+list);
        String JsonStr=new Gson().toJson(list);
        try {
            response.setContentType("charset=UTF-8");
            response.getWriter().write(JsonStr);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @RequestMapping("getVagueBlog")
    public void getVagueBlog(@RequestParam Map<String, String> map,HttpServletResponse response) throws IOException {
        String type=map.get("type");
        String content=map.get("content");
        //int pageNo= Integer.parseInt(map.get("pageNo"));
        List<Blog> list=blogService.selecVague(content,type);
        System.out.println(list);
        if(list.size()>0){
            String JsonStr=new Gson().toJson(list);
            response.setContentType("charset=UTF-8");
            response.getWriter().write(JsonStr); }
        else {
        String JsonStr="faild";
        response.setContentType("charset=UTF-8");
        response.getWriter().write(JsonStr);

          }
    }

    @CrossOrigin
    @RequestMapping("selectWebBlog")
    public void selectWebBlog(HttpServletResponse response){
        System.out.println("页面数据查看被调用");
        List<Blog> blogList = new ArrayList<Blog>();
        String blogStr = "";
        blogList = blogService.selectBlog();
        System.out.println(blogList.size());
        blogStr = new Gson().toJson(blogList);
        System.out.println(blogStr);
        try {
            response.setContentType("charset=UTF-8");
            response.getWriter().write(blogStr);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @CrossOrigin
    @RequestMapping("deletWebBlog")
    public void delet(HttpServletResponse response,String id) throws IOException {
        System.out.println(id);
        blogService.delete(id);
        response.getWriter().write("success");

    }
    @CrossOrigin
    @RequestMapping("selectBlogById")
    public void selectBlogById(HttpServletResponse response,String blogid) throws IOException {

        Blog blog =blogService.selectById(blogid);
        String JsonStr = new Gson().toJson(blog);
        response.getWriter().write("success");

    }
    @CrossOrigin
    @RequestMapping("Audit")
    public void Audit(HttpServletResponse response,String blogid) throws IOException {
        System.out.println("审核调用");
        String stu="已审核";
        blogService.audit(blogid,stu);


    }
}


